package com.syas.common.utilities;

import com.syas.web.models.FrmgerPfmceSearchModel;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
/**
 * Created by weigong on 2015/12/3.
 */
public class ArrayToObject {

    /***
     * @param os
     * @param clazz
     * @deprecated  clazz对象要提供字段全参的为Object的构造方法，这个条件是必要的，否则转不成功
     * @return
     */
    public static Object getObjectByArray(Object [] os,Class clazz){
        Field [] fields = clazz.getDeclaredFields();
        Class[] argtype = new Class[fields.length];//代表构造方法的参数类型数组
        Object obj = null;
        if(os.length == fields.length) {
            //遍历数组的字段
            for (int j = 0; j < fields.length; j++) {
                fields[j].setAccessible(true);
                argtype[j] = Object.class;
            }
            Constructor con;
            try {
                con = clazz.getDeclaredConstructor(argtype);
                obj = con.newInstance(os);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return obj;
    }


}
